﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MTGTools
{
    public class GUICardPoolDisplay
    {
        public const int DISPLAY_CARD_STACK_MAX = 6;

        int DISPLAY_CARD_STACK_SPACE = 24;

        private Form parentForm;
        private int positionX;
        private int positionY;
        private int stackDepth;
        private int DISPLAY_CARD_HEIGHT;
        private int DISPLAY_CARD_WIDTH;

        LinkedList<PictureBox> cardPictures = new LinkedList<PictureBox>();

        public GUICardPoolDisplay(Form parentForm, int positionX, int positionY, int stackDepth, double scale)
        {
            this.parentForm = parentForm;
            this.positionX = positionX;
            this.positionY = positionY;
            this.stackDepth = stackDepth;
            DISPLAY_CARD_HEIGHT = (int)(680 * scale);
            DISPLAY_CARD_WIDTH = (int)(480 * scale);
            DISPLAY_CARD_STACK_SPACE = (int)(DISPLAY_CARD_STACK_SPACE * scale);
        }

        public void updateImages(string[] cardArray)
        {
            Die();

            int currentCard = 0;
            int stacksNeeded = (int)Math.Ceiling(cardArray.Length / (double)stackDepth);

            for (int w = 0; w < stacksNeeded; w++)
            {
                int currentStartIndex = w * stackDepth;
                int cardsInStack = stackDepth;

                if (w == (stacksNeeded - 1))
                    cardsInStack = cardArray.Length % stackDepth;

                for (int h = (cardsInStack - 1); h > -1; h--)
                {
                    PictureBox newCard = new System.Windows.Forms.PictureBox();
                    int cardPositionX = positionX + (DISPLAY_CARD_WIDTH + DISPLAY_CARD_STACK_SPACE) * w;
                    int cardPositionY = positionY + (DISPLAY_CARD_STACK_SPACE * (h % stackDepth));

                    newCard.Image = GUICacheManager.getImage(cardArray[currentCard]);
                    newCard.Location = new System.Drawing.Point(cardPositionX, cardPositionY);
                    newCard.Size = new System.Drawing.Size(DISPLAY_CARD_WIDTH, DISPLAY_CARD_HEIGHT);
                    newCard.SizeMode = PictureBoxSizeMode.StretchImage;

                    currentCard++; //yes, we could calculate this instead of counting, but... SCREW THAT.

                    cardPictures.AddLast(newCard);
                    parentForm.Controls.Add(newCard);
                }
            }
        }

        public void Die()
        {
            foreach (PictureBox picture in cardPictures)
            {
                parentForm.Controls.Remove(picture);
            }

            //remove picture boxes from list.
            cardPictures = new LinkedList<PictureBox>();
        }
    }
}
